/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.common.json;

import com.entities.EventInfo;
import com.entities.EventLibrary;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author Tuan Long
 */
public class EventLibraryJson {

    /**
     * @EventLibraryJson.convertEventLibraryDataToJSONObject : create message
     * JsonObject type from EventLibrary object
     * @param obj
     * @return JSONObject
     */
    public static JSONObject convertEventLibraryDataToJsonObject(EventLibrary obj) {
        JSONObject message = new JSONObject();
        message.put("eventId", obj.getEventID());
        message.put("name", obj.getEventName());
        message.put("description", obj.getDescription());
        message.put("eventType", obj.getEventType());
        message.put("start", obj.getTimeStart());
        message.put("end", obj.getTimeEnd());
        return message;
    }

    /**
     * @EventLibraryJson.convertJSONObjectStringToEventLibrary
     * @param jsonstr
     * @return EventLibrary
     */
    public static EventLibrary convertJSONObjectStringToEventLibrary(String jsonstr) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(jsonstr);
            String idstr = json.get("eventID").toString();
            int id = Integer.parseInt(idstr);
            String name = json.get("eventName").toString();
            String des = json.get("description").toString();
            String type = json.get("eventType").toString();
            String timeS = json.get("start").toString();
            String timeE = json.get("end").toString();  
            String avatar = json.get("avatar").toString();
            String shortdes = json.get("shortDes").toString();
            String created = json.get("created").toString();
            String address = json.get("address").toString();
            EventLibrary obj = new EventLibrary(id, name, des, type, timeS, timeE);
            obj.setAvatar(avatar);
            obj.setShortdes(shortdes);
            obj.setCreated(created);
            obj.setAddress(address);
            return obj;
        } catch (ParseException ex) {
            Logger.getLogger(EventLibraryJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    /**
     * @EventLibraryJson.convertJSONObjectStringToEventLibraryForFillEventsDataTable
     * @param jsonstr
     * @return EventLibrary
     */
    public static EventLibrary convertJSONObjectStringToEventLibraryForFillEventsDataTable(String jsonstr) {
        try {            
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(jsonstr);            
            String idstr = json.get("eventId").toString();           
            int id = Integer.parseInt(idstr);            
            String name = json.get("name").toString();            
            String des = json.get("description").toString();            
            String type = json.get("eventType").toString();            
            String timeS = json.get("start").toString();            
            String timeE = json.get("end").toString();              
            String registeredStr = json.get("registered").toString();            
            int reg = Integer.parseInt(registeredStr);
            EventLibrary obj = new EventLibrary(id, name, des, type, timeS, timeE,reg);
            return obj;
        } catch (ParseException ex) {
            Logger.getLogger(EventLibraryJson.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static JSONObject convertEventLibraryAndInfoDataToJsonObject(EventLibrary obj, EventInfo ei) {
        JSONObject message = new JSONObject();
        message.put("eventId", obj.getEventID());
        message.put("name", obj.getEventName());
        message.put("description", obj.getDescription());
        message.put("eventType", obj.getEventType());
        message.put("start", obj.getTimeStart());
        message.put("end", obj.getTimeEnd());
        message.put("avatar", ei.getAvatar());
        message.put("shortdes", ei.getShortdes());
        message.put("address", ei.getAddress());
        return message;
    }
}
